package com.ym.controller.doctor.self;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.common.enums.UserUserTypeEnum;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.DoctorServiceItemEntity;
import com.ym.medical.domain.entity.PhoneRuleEntity;
import com.ym.medical.domain.entity.SysDictEntity;
import com.ym.medical.domain.entity.TextRuleEntity;
import com.ym.medical.domain.param.DoctorPhoneSettingModel;
import com.ym.medical.domain.param.TextRuleParam;
import com.ym.medical.domain.vo.DoctPhoneRuleItemVo;
import com.ym.medical.domain.vo.DoctorServicePriceVo;
import com.ym.medical.domain.vo.GraphicSetVo;
import com.ym.medical.service.IDoctorServiceItemSV;
import com.ym.medical.service.IDoctorServicePriceSV;
import com.ym.medical.service.ISysDictSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 张文博
 * @date : 2019/5/9 14:33
 */
@Api("医生端-医生服务项")
@Controller
@ApiSign
@RequestMapping(value = "doctor/self/service",headers = Constants.Api.VERSION)
public class DoctorServiceItemController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(DoctorServiceItemController.class);

	@Autowired
	private IDoctorServiceItemSV iDoctorServiceItemSV;

	@Autowired
	private ISysDictSV iSysDictSV;

	@Autowired
	private IDoctorServicePriceSV iDoctorServicePriceSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("查询服务项")
	@GetMapping("/getSrviceItem")
	@ResponseBody
	public APIResponse<Map<String, Object>> getSrviceItem(
			@ApiParam(name = "doctId", value = "医生id(01图文问诊02电话咨询03私人医生04视频问诊)", required = true)
			@RequestParam Long doctId) throws Exception {
		Map<String, Object> map = new HashMap<>();
		Long doctId2 = ShiroUtil.getLoginId();
		List<DoctorServiceItemEntity> doctorServiceItems = iDoctorServiceItemSV.getSrviceItem(doctId2);
		String dictKey = "DoctorServiceItemEnum";
		Map<String, String> dictValues = iSysDictSV.getDictValuesToMap(dictKey);
		DoctorServicePriceVo freeClinicPrice = iDoctorServicePriceSV.freeClinicPrice(doctId);
		map.put("doctorServiceItems", doctorServiceItems);
		map.put("dictValues", dictValues);
		map.put("freeClinic", freeClinicPrice == null ? new String() : freeClinicPrice.getServiceState());
		return successData(map);
	}

	@Log
	@ApiOperation("医生服务项 启用未用")
	@PostMapping(value = "/DisOrEnableItem", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse DisOrEnableItem(
			@ApiParam(name = "doctorServiceItems", value = "服务项对象", required = true)
			@RequestBody DoctorServiceItemEntity doctorServiceItem) throws Exception {
		Long doctId = ShiroUtil.getLoginId();
		doctorServiceItem.setDoctId(doctId);
		Map<String, Object> map = iDoctorServiceItemSV.disOrEnableItem(doctorServiceItem);
		if (map.get("docServId") != null) {
			return successData(map.get("docServId"));
		} else if (map.get("rows") != null) {

			return successMsg("操作成功");
		}
		return failMsg("操作失败");
	}

//    @ApiOperation("查询图文问诊设置和单价单条")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = Global.AUTHORIZATION, value = "token值", required = true, paramType = "header", dataType = "String")
//    })
//    @GetMapping(value = "/selectTextSetPrice")
//    @ResponseBody
//    public APIResponse selectTextSetPrice(
//            @ApiParam(name = "docServId", value = "服务项id long 类型", required = true)
//                  @RequestParam  Long docServId)throws Exception{
//        Map<String,Object> map = new HashMap<>();
//        DoctorTextSetting doctorTextSetting = iDoctorServiceItemSV.selectTextSet(docServId);
//        DoctorServicePrice doctorServicePrice = iDoctorServiceItemSV.selectSingleServicePrice(docServId);
//        map.put("textSe",doctorTextSetting);
//        map.put("servicePrice",doctorServicePrice);
//        return APIResponse.success(map);
//    }

	@Log(dataBase = false, printBody = false)
	@ApiOperation("查询图文问诊设置/规则/单价列表")
	@GetMapping(value = "/selectTextRulePrice")
	@ResponseBody
	public APIResponse<Map<String, Object>> selectTextRulePrice(
			@ApiParam(name = "docServId", value = "服务项id long 类型", required = true)
			@RequestParam Long docServId) throws Exception {
		Map<String, Object> map = new HashMap<>();
		//图文设置
		//DoctorTextSetting doctorTextSetting = iDoctorServiceItemSV.selectTextSet(docServId);
		//规则对应价格列表
		Long doctId = ShiroUtil.getLoginId();
		List<DoctorServicePriceVo> doctorServicePrice = iDoctorServiceItemSV.selectServicePrice(doctId);
		//规则列表
		List<TextRuleEntity> textRules = iDoctorServiceItemSV.selectDefaultItem();
		//map.put("doctorTextSetting", doctorTextSetting);
		map.put("doctorServicePrice", doctorServicePrice);
		map.put("textRules", textRules);
		return successData(map);
	}


	@Log
	@ApiOperation("医生图文问诊咨询设置 (规则,单价,图文设置)")
	@PostMapping(value = "/graphicSet", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse<GraphicSetVo> graphicSet(
			@ApiParam(name = "textRuleModel", value = "图文设置对象", required = true)
			@RequestBody GraphicSetVo textRuleModel) throws Exception {
		Long doctId = ShiroUtil.getLoginId();
		DoctorServiceItemEntity serviceItem = textRuleModel.getDoctorServiceItem();
		serviceItem.setDoctId(doctId);
		TextRuleParam ruleModel = textRuleModel.getTextRuleModel();
		int rows = iDoctorServiceItemSV.graphicSet(serviceItem, ruleModel);
		if (rows != 1) {
			return successMsg("请重试");
		}
		return successMsg("ok");
	}

	@Log
	@ApiOperation("删除图文规则")
	@GetMapping(value = "/deleteTextRule")
	@ResponseBody
	public APIResponse deleteTextRule(
			@ApiParam(name = "textRuleId", value = "图文规则id 单个删除", required = true)
					Long textRuleId) throws Exception {
		int rows = iDoctorServiceItemSV.deleteTextRule(textRuleId);
		if (rows != 1) {
			return failMsg("操作失败");
		}

		return successMsg("操作成功");
	}


	//分割线----------------------------------------------------------------------------------
/*    @ApiOperation("通话规则和单价查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Global.AUTHORIZATION, value = "token值", required = true, paramType = "header", dataType = "String")
    })
    @GetMapping(value = "/getPhoneRulePrice")
    @ResponseBody
    public APIResponse getPhoneRulePrice(
            @ApiParam(name = "docServId", value = "服务项id", required = true)
            Long docServId)throws Exception{
        DoctorServicePrice doctorServicePrice = iDoctorServiceItemSV.selectPhoneRulePrice(docServId);
        return successData(doctorServicePrice);
    }*/

	@Log(dataBase = false, printBody = false)
	@ApiOperation("医生预约时间表查询")
	@GetMapping("/getTimeTable")
	@ResponseBody
	public APIResponse<List<SysDictEntity>> getTimeCode(
			@ApiParam(name = "docServId", value = "服务项id", required = true)
			@RequestParam Long docServId) throws Exception {

		List<SysDictEntity> sysDictList = iSysDictSV.getTimeTable(docServId);
		return successData(sysDictList);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("通话规则和单价查询列表")
	@GetMapping(value = "/getPhoneRulePrices")
	@ResponseBody
	public APIResponse<Map<String, Object>> selectPhoneRulePrice(
			@ApiParam(name = "docServId", value = "服务项id", required = true)
			@RequestParam Long docServId) throws Exception {
		Map<String, Object> map = new HashMap<>();
		Long doctId = null;
		//String dictKey = "DctorPhoneRuleEnum";
		String userType = ShiroUtil.getLoginUser().getUserTypeEnum().getKey();
		if (UserUserTypeEnum.DOCTOR.getKey().equals(userType)){
			doctId = ShiroUtil.getLoginId();
		}else if (UserUserTypeEnum.PATIENT.getKey().equals(userType)){
			DoctorServiceItemEntity doctorServiceItem = iDoctorServiceItemSV.selectById(docServId);
			doctId = doctorServiceItem.getDoctId();
		}
		List<DoctorServicePriceVo> phoneRulePrices =
				//规则对应的单价
				iDoctorServiceItemSV.selectPhoneRulePrices(doctId);
		//Map<String, String> dictValues =iSysDictSV.getDictValues(dictKey);
		//默认的规则查询
		List<PhoneRuleEntity> phoneRules = iDoctorServiceItemSV.getDefaultPhoneRule();
		map.put("phoneRules", phoneRules);
		map.put("PhoneRulePrices", phoneRulePrices);
		return successData(map);
	}


	@Log
	@ApiOperation("电话规则设置含单价以及单价更改")
	@PostMapping(value = "/upPhoneRuleAndPrice", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse<DoctPhoneRuleItemVo> setPhoneRulePrice(
			@ApiParam(name = "phoneRuleModel", value = "电话规则对象含单价", required = true)
			@RequestBody DoctPhoneRuleItemVo phoneRuleModel) throws Exception {
		Long doctId = ShiroUtil.getLoginId();
		phoneRuleModel.getDoctorServiceItem().setDoctId(doctId);
		int rows = iDoctorServiceItemSV.upPhoneRuleAndPrice(phoneRuleModel);
		if (rows == 0) {
			return failMsg("操作失败");
		}
		return successMsg("操作成功");
	}

	@Log
	@ApiOperation("通话时间设置")
	@PostMapping(value = "/phoneSetting", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse<List<DoctorPhoneSettingModel>> phoneSetting(
			@ApiParam(name = "doctorPhoneSetting", value = "电话设置对象集合", required = true)
			@RequestBody List<DoctorPhoneSettingModel> doctorPhoneSettingModel) throws Exception {
		int rows = iDoctorServiceItemSV.phoneSetting(doctorPhoneSettingModel);
		if (rows != 1) {
			return failMsg("操作失败");
		}

		return successMsg("操作成功");
	}


	@Log
	@ApiOperation("电话相关设置是否完整")
	@GetMapping(value = "/effectiveState")
	@ResponseBody
	public APIResponse phoneEffectiveState(
			@RequestParam(required = true, name = "doctServId") Long doctServId) throws Exception {
		int rows = iDoctorServiceItemSV.phoneEffectiveState(doctServId);
		return success();
	}


//	@Log
//	@ApiOperation("视频咨询收费规则设置")
//	@PostMapping(value = "/videoTime/set", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
//	@ResponseBody
//	public APIResponse videoTimeSet(
//			@Valid @RequestBody VideoTimeSetDTO videoTimeSetDTO
//	) throws Exception {
//		iDoctorServiceItemSV.videoTimeSet(videoTimeSetDTO);
//		return success();
//	}

//	@Log
//	@ApiOperation("视频咨询收费设置查询")
//	@GetMapping(value = "/videoTime/set")
//	@ResponseBody
//	public APIResponse<VideoTimeSetVO> videoTimeGet() {
//		User currentUser = getCurrentUser();
//		VideoTimeSetVO videoTimeSetVO = iDoctorServiceItemSV.videoTimeGet(currentUser.getId());
//		return successData(videoTimeSetVO);
//	}

	@Log
	@ApiOperation("查询医生义诊服务历史记录")
	@GetMapping(value = "/freeClinic/list")
	@ResponseBody
	public APIResponse freeClinicList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "pageSize", value = "每页数量", required = false)
			@RequestParam(name = "pageSize", required = false, defaultValue = "10")
					int pageSize) {
		Long doctId = ShiroUtil.getLoginId();
		PageInfo<DoctorServicePriceVo> freeClinicList = iDoctorServicePriceSV.freeClinicList(pageNo, pageSize, doctId);
		return successData(freeClinicList);
	}

	@Log
	@ApiOperation("义诊查询该医生效的的图文单价")
	@GetMapping(value = "/effective/price")
	@ResponseBody
	public APIResponse<Map<String, Object>> effectivePrice() {
		Long doctId = ShiroUtil.getLoginId();
		Map<String, Object> map = new HashMap<>();
		DoctorServicePriceVo effectivePrice = iDoctorServicePriceSV.effectivePrice(doctId);
		DoctorServicePriceVo freeClinicPrice = iDoctorServicePriceSV.freeClinicPrice(doctId);
		map.put("effectivePrice", effectivePrice);
		map.put("freeClinicPrice", freeClinicPrice == null ? new DoctorServicePriceVo() : freeClinicPrice);
		return successData(map);
	}


	@Log
	@ApiOperation("提交图文义诊服务")
	@PostMapping(value = "/graphic/freeClinic")
	@ResponseBody
	public APIResponse<TextRuleParam> freeClinic(
			@RequestBody TextRuleParam textRuleModel) {
		Long doctId = ShiroUtil.getLoginId();
		int rows = iDoctorServiceItemSV.freeClinic(textRuleModel, doctId);
		if (rows == 0) {
			return failMsg("提交失败");
		}
		return successMsg("提交成功");
	}

	@Log
	@ApiOperation("图文义诊服务修改")
	@PostMapping(value = "/update/freeClinic")
	@ResponseBody
	public APIResponse upFreeClinic(
			@RequestBody TextRuleParam textRuleModel) {
		Long doctId = ShiroUtil.getLoginId();
		int rows = iDoctorServiceItemSV.upFreeClinic(textRuleModel, doctId);
		if (rows == 0) {
			return failMsg("修改失败");
		}
		return successMsg("修改成功");

	}


}
